package drh.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.handlers.ScalarHandler;

import drh.bean.Order;
import drh.dao.BaseDao;
import drh.dao.OrderDao;
import handler.OrderResultSetHandler;

public class OrderDaoImpl extends BaseDao implements OrderDao {

	@Override
	public Integer deleteOrderById(Integer id) {
		try {
			return query.update("delete from orders where id=?",id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public Integer updateOrder(Order info) {
		try {
			return query.update("update orders set  orderuid=?,state=?,payway=?,userid=?,addressid=?,message=?,money=?,time=?,isdelete=?,integral=? where id=? ",
					info.getOrderuid(),info.getState(),info.getPayway(),info.getUserid(),info.getAddressid(),
					info.getMessage(),info.getMoney(),info.getTime(),info.getIsdelete(),info.getIntegral(),info.getId());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public Integer addOrder(Order info) {
		try {
			return query.update("insert into orders(orderuid,state,payway,userid,addressid,message,money,time,isdelete,integral) values(?,?,?,?,?,?,?,?,?,?)",
					info.getOrderuid(),info.getState(),info.getPayway(),info.getUserid(),info.getAddressid(),info.getMessage(),info.getMoney(),info.getTime(),
					info.getIsdelete(),info.getIntegral());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public List<Order> GetOrderPaging(Integer pageIndex, Integer pageSize) {
		String sql ="select * from orders limit ?,?";
		//select o.*,a.*,a.id as \"aid\",a.userid as \"auserid\" from orders o,address a where o.addressid=a.id limit ?,?
		try {
			return query.query(sql, new OrderResultSetHandler(),(pageIndex-1)*pageSize,pageSize);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return null;
	}

	@Override
	public Integer getCount() {
		String sql = "select count(1) from orders";
		Object query2;
		try {
			 query2 = query.query(sql, new ScalarHandler());
			return Integer.parseInt(query2.toString());
		} catch (SQLException e) {
	
			e.printStackTrace();
		}
		
		return null;
	}

	
}
